package com.quanyan.authority.web;

import com.lefu.commons.utils.Page;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.authority.constants.AuthorityConstants;
import com.quanyan.authority.model.db.TbAdminUser;
import com.quanyan.authority.model.db.TbFunction;
import com.quanyan.authority.model.request.ReqFunction;
import com.quanyan.authority.service.FunctionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/4.
 */
@RestController
@RequestMapping("admin/api/authority/function/api/")
public class FunctionController extends BaseController<ReqFunction> {

    public static final String QUERY_BY_ID = "queryById";
    private static final Logger logger = LoggerFactory.getLogger(FunctionController.class);

    @Autowired
    private FunctionService functionService;

    @Autowired
    private HttpServletRequest request;


    @RequestMapping(value = "userFunction",method = RequestMethod.POST)
    public APIResponse getUserFunction(HttpServletRequest request){
       TbAdminUser tbAdminUser =(TbAdminUser) request.getSession().getAttribute(AuthorityConstants.USER_SESSION_KEY);
        try {
            List<TbFunction>  lst = functionService.getUserFunction(tbAdminUser.getId());
            return APIResponse.returnSuccess(lst);
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常！");
        }
    }

    @Override
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public APIResponse save(@RequestBody @Valid ReqFunction reqFunction, BindingResult bindingResult) {
        if(bindingResult.hasErrors())
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        try {
            int uid = (Integer.parseInt(request.getAttribute("adminUid").toString()));
            reqFunction.setFounder(uid);
            functionService.add(reqFunction);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常");
        }
    }

    @Override
    @RequestMapping(value = "query",method = RequestMethod.POST)
    public APIResponse query(@RequestBody ReqFunction reqFunction) {
        try {
            Page page = functionService.findAll(reqFunction, reqFunction);
            return APIResponse.returnSuccess(page);
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常");
        }
    }

    @Override
    @RequestMapping(value = "queryById",method = RequestMethod.POST)
    public APIResponse queryById(@RequestBody Map map) {
        try {
            Integer id = (Integer.parseInt( map.get("id").toString()));
            if(id == null)
                return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
            return APIResponse.returnSuccess(functionService.findById(id));
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail("系统异常！");
        }
    }

    @Override
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public APIResponse update(@RequestBody ReqFunction reqFunction) {
        try {
            functionService.update(reqFunction);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail("系统异常！");
        }
    }

    @Override
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public APIResponse deleteById(@RequestBody Map map) {
        try {
            Integer id = (Integer.parseInt( map.get("id").toString()));
            if(id == null)
                return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
            functionService.del(id);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail("系统异常！");
        }
    }
}
